<template>
  <view>
    <my-search @click="gotoSearch"></my-search>
  <view class="scoll-viwe-container">
     <!-- 左边华东兰 -->
      <scroll-view scroll-y="true" class="left-scroll-view" :style="{height: wh + 'px'}">
        <block v-for="(item,index) in cateList" :key="index">
        <view class="left-scroll-view-item" :class="{'active': active===index ? 'active':''}" @click="isactive(index)">
       {{item.cat_name}}
     </view>
      </block>
      
      </scroll-view>
      
       <!-- // 右边华东兰 -->
      <scroll-view :scroll-top="scrollTop" scroll-y="true" class="right-scroll-view" :style="{height: wh + 'px'}">
     <view class="cate-lv2"v-for="(item1,index1) in cateLevel2" :key="index1">
       <view class="cate-lv2-title">
         / {{item1.cat_name}} /
       </view>
       <view class="cate-lv3-list">
         <view class="cate-lv3-item" v-for="(item2,index2) in item1.children" :key="index2" @click="gotoGoodsList(item2)">
           <image :src="item2.cat_icon" mode=""></image>
           <text>{{item2.cat_name}}</text>
         </view>
       </view>
       
       
     </view>
       </scroll-view>
  </view>
  </view>
</template>

<script>
  import tabbarBadge  from '@/mixins/tabbar-badge.js'
  export default {
    name: 'cate',
    components:{
    },
    mixins:[tabbarBadge],
    data() {
      return {
        num:300,
        active:0,
        wh: 0 ,// 屏幕可视高度
        cateList: [], // 分类列表
        cateLevel2: [], //  2级分类
        scrollTop: 0 // 右侧滚动位置
      };
    },
    created(){
    },
    mounted(){
    },
    onLoad() {
      // 获取屏幕的可视高度
      let info = uni.getSystemInfoSync()
      this.wh = info.windowHeight - 50
      console.log( this.wh)
      this.getCateList()
    },
    methods:{
      // 跳转到搜索页面
      gotoSearch(){
        console.log('gotoSearch')
        uni.navigateTo({
          url:'../../subpkg/search/search'
        })
      },
      // 跳转商品列表页面
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+ item.cat_id
        })
      },
      // 切换选中项
      isactive(i){
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 1: 0
      },
     async getCateList() {
       const { data: res } = await uni.$http.get('/api/public/v1/categories')
       if (res.meta.status !== 200) return uni.$showMsg()
       this.cateList = res.message
       this.cateLevel2 = this.cateList[0].children
     }
    }
  }
</script>

<style lang="scss">
   .scoll-viwe-container {
      display: flex;
      
      .left-scroll-view {
        width: 120px;
        
       .left-scroll-view-item {
         line-height: 60px;
         background-color: #f7f7f7;
         text-align: center;
         font-size: 12px;
         &.active {
           background-color: #ffffff;
           position: relative;
           // 渲染激活项左侧的红色指示边线
           &::before {
             content: ' ';
             display: block;
             width: 3px;
             height: 30px;
             background-color: #c00000;
             position: absolute;
             left: 0;
             top: 50%;
             transform: translateY(-50%);
           }
         }
       }
      }
      
      .cate-lv2-title{
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        padding: 15px 0;
      }
      
      .cate-lv3-list {
        display: flex;
        flex-wrap: wrap;
      
        .cate-lv3-item {
          width: 33.33%;
          margin-bottom: 10px;
          display: flex;
          flex-direction: column;
          align-items: center;
      
          image {
            width: 60px;
            height: 60px;
          }
      
          text {
            font-size: 12px;
          }
        }
      }
      }
      
</style>
